package com.hj.saapp.control.activity;

import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;

import com.hj.saapp.R;
import com.hj.saapp.common.event.EventSetDevice;
import com.hj.saapp.common.utils.ClsUtils;
import com.hj.saapp.control.activity.base.BaseActivity;
import com.hj.saapp.control.adapter.CommonAdapter;
import com.hj.saapp.control.adapter.PrintSetingAdapter;
import com.hj.saapp.control.service.BluetoothService;

import java.lang.reflect.Method;
import java.util.Timer;
import java.util.TimerTask;

import de.greenrobot.event.EventBus;

import static com.pgyersdk.conf.a.m;

public class PrintSetingActivity extends BaseActivity implements BluetoothService.BluetoothImp, View.OnClickListener, AdapterView.OnItemClickListener {

    private BluetoothService bluetoothService = null;
    ProgressDialog progressDialog = null;
    ProgressDialog progressPDDialog = null;
    TextView subBtn;

    @Override
    public void initView() {
        subBtn = (TextView) findViewById(R.id.subBtn);
    }


    @Override
    public int getLayoutByid() {
        return R.layout.activity_print_seting;
    }

    @Override
    public int getTitleById() {
        return R.string.activity_print_seting;
    }

    @Override
    public void initData() {

        bluetoothService = BluetoothService.getBluetoothService(this);
        bluetoothService.register();
        initBlue();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter
                .getDefaultAdapter();

        bluetoothAdapter.cancelDiscovery();
        bluetoothService.unregister();
    }

    @Override
    public CommonAdapter getAdapter() {
        return new PrintSetingAdapter(mData);
    }

    @Override
    public void found(BluetoothDevice dvice) {

        String addrs[] = dvice.getAddress().split(":");

        if (addrs != null && ("03").equals(addrs[1])) {

            boolean isHas = false;
            for (int i = 0; i < mData.size(); i++) {

                BluetoothDevice d = (BluetoothDevice) mData.get(i);
                if (dvice.equals(d)) {
                    isHas = true;
                }

            }
            if (!isHas) {
                mData.add(dvice);
                adapter.notifyDataSetChanged();
            }
        }





    }

    @Override
    public void initListener() {
        subBtn.setOnClickListener(this);
        queryLv.setOnItemClickListener(this);
    }

    @Override
    public void scanStart() {

        if (this.isFinishing()) {
            return;
        }

        subBtn.setText("搜索蓝牙设备中。。。");
        subBtn.setEnabled(false);
        mData.clear();
        adapter.notifyDataSetChanged();
        progressDialog = ProgressDialog.show(this, "请稍等。。。", "搜索蓝牙设备中。。。", true);
        progressDialog.setCanceledOnTouchOutside(true);
        progressDialog.setCancelable(true);


    }

    @Override
    public void pairStart() {

        adapter.notifyDataSetChanged();

    }

    @Override
    public void scanFinish() {

        if (this.isFinishing()) {
            return;
        }
        subBtn.setEnabled(true);
        subBtn.setText("扫描蓝牙设备");
        if (progressDialog != null) {

            progressDialog.dismiss();
        }
        adapter.notifyDataSetChanged();

        if (mData.size() == 0) {
            showPopDialog("没有找到任何蓝牙设备");
        }

    }

    @Override
    public void pairFinish() {
        adapter.notifyDataSetChanged();

    }

    @Override
    public void pairError() {
        adapter.notifyDataSetChanged();

    }


    @Override
    public void on() {

        initBlue();


    }

    @Override
    public void off() {

        initBlue();

    }

    @Override
    public void request() {

        if (progressPDDialog != null) {
            progressPDDialog.dismiss();

        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.subBtn:

                initBlue();

                break;
        }
    }

    public void initBlue() {
        if (bluetoothService.isOpen()) {

            bluetoothService.searchDevices();

        } else {
            bluetoothService.openBluetooth();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        BluetoothDevice bluetoothDevice = (BluetoothDevice) mData.get(position - 1);

        if (bluetoothDevice.getBondState() != BluetoothDevice.BOND_BONDED) {
            if (progressPDDialog == null) {
                progressPDDialog = ProgressDialog.show(this, "请稍等。。。", "正在配对。。。", true);
                progressPDDialog.setCanceledOnTouchOutside(true);
                progressPDDialog.setCancelable(true);
            } else {
                progressPDDialog.show();
            }


            try {
                if (ClsUtils.pair(bluetoothDevice)) {


                } else {
                    if (progressPDDialog != null) {
                        progressPDDialog.dismiss();
                    }
                    showPopDialog("配对失败");
                }

            } catch (Exception e) {

            }
        }
        else
        {
            EventSetDevice e = new EventSetDevice();
            e.addr = bluetoothDevice.getAddress();
            EventBus.getDefault().post(e);
            this.finish();
        }


    }


    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            if (msg.what == 1) {

                for (int i = 0; i < mData.size(); i++) {

                    BluetoothDevice b = (BluetoothDevice) mData.get(i);
                    Log.e("test", "status" + b.getBondState());

                }
//                adapter.notifyDataSetChanged();

            }
            super.handleMessage(msg);
        }

        ;
    };


}
